﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class SysCodeList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F1082;
        protected GridView gvSysCode;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2SysCodeList(SysCodeQueryEntity sysCodeQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(sysCodeQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(sysCodeQuery, "SYS_CODE");
            base.BindData2GridView(this.gvSysCode, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSysCode, "SYS_CODE_TYPE");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1100_006");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "SYS_CODE") + base.GetGlobalMsgResource("F1800_002");
                    this.LoadData();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_003") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSysCode, "SYS_CODE_TYPE");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_006");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_007");
            }
            else
            {
                base.Response.Redirect(string.Format("SysCode.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity syscodeQuery = this.GetSysCodeQueryEntity();
            syscodeQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = syscodeQuery.CurrentPage;
            syscodeQuery.CurrentPage = "0";
            this.gvSysCode.AllowSorting = false;
            this.BindData2SysCodeList(syscodeQuery);
            this.gvSysCode.ShowFooter = false;
            this.hdnActionID.Value = "";
            syscodeQuery.IsGetAll = false;
            syscodeQuery.CurrentPage = currentPageIndex;
            this.SetSysCodeQueryEntity(syscodeQuery);
            base.Export2Excel("SysCode.xls", this.gvSysCode);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
            SysCodeQuery.CurrentPage = "0";
            this.BindData2SysCodeList(SysCodeQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
            SysCodeQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SysCodeList(SysCodeQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
            SysCodeQuery.SysCodeType = "";
            this.BindData2SysCodeList(SysCodeQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
            if (int.Parse(SysCodeQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                SysCodeQuery.CurrentPage = (int.Parse(SysCodeQuery.CurrentPage) + 1).ToString();
                this.BindData2SysCodeList(SysCodeQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
            if (int.Parse(SysCodeQuery.CurrentPage) > 0)
            {
                SysCodeQuery.CurrentPage = (int.Parse(SysCodeQuery.CurrentPage) - 1).ToString();
                this.BindData2SysCodeList(SysCodeQuery);
            }
        }

        private SysCodeQueryEntity GetSysCodeQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as SysCodeQueryEntity);
            }
            return (base.PageQueryEntitySession as SysCodeQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                SysCodeQueryEntity SysCodeQuery = this.GetSysCodeQueryEntity();
                SysCodeQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SysCodeList(SysCodeQuery);
            }
        }

        protected void gvSysCode_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1082", this.gvSysCode);
        }

        protected void gvSysCode_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvSysCode_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                SysCodeQueryEntity sysCode = this.GetSysCodeQueryEntity();
                sysCode.SortField = e.SortExpression;
                if (sysCode.SortDirection == "ASC")
                {
                    sysCode.SortDirection = "DESC";
                }
                else if (sysCode.SortDirection == "DESC")
                {
                    sysCode.SortDirection = "ASC";
                }
                this.BindData2SysCodeList(sysCode);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            SysCodeQueryEntity sysCodeQuery = new SysCodeQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    sysCodeQuery.SysCodeType = this.PreviousPage.SysCode;
                    sysCodeQuery.Description = this.PreviousPage.Description;
                }
                else
                {
                    sysCodeQuery.SysCodeType = "";
                    sysCodeQuery.Description = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            sysCodeQuery.IsGetAll = false;
            sysCodeQuery.CurrentPage = "0";
            sysCodeQuery.PageSize = this.gvSysCode.PageSize.ToString();
            sysCodeQuery.SortField = "SC.ROWID";
            sysCodeQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvSysCode.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetSysCodeQueryEntity(sysCodeQuery);
            this.BindData2SysCodeList(sysCodeQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSysCodeQueryEntity(SysCodeQueryEntity sysCodeQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = sysCodeQuery;
            }
            else
            {
                base.PageQueryEntitySession = sysCodeQuery;
            }
        }

        public SysCodeQuery PreviousPage
        {
            get
            {
                return (SysCodeQuery) base.PreviousPage;
            }
        }
    }
}

